import {getDicts} from '@/api/sys.js'

export const dictMixin = {
    data() {
        return {
            dictOptions: {}
        };
    },
    methods: {
        useDict(dicts, initDict) {
			if(!Array.isArray(dicts)){
				if(this.dictOptions[dictType]){
					if(initDict != undefined && initDict != null){
						initDict();
					}
				}else{
					getDicts(dictType).then(response=>{
						this.dictOptions[dictType] = response.data;
						if(initDict != undefined && initDict != null){
						  	initDict();
						}
					})	
				}
				return;
			}
			
            const requests = dicts.map(dictType=>{
				if(this.dictOptions[dictType]){
					return new Promise((resolve, reject) => {
						resolve({data:this.dictOptions[dictType]});
					});;
				}
				return getDicts(dictType).then(response=>{
					  this.dictOptions[dictType] = response.data;
				})	
			})
			Promise.all(requests).then(results => {
				if(initDict != undefined && initDict != null){
					initDict();
				}
			});
        },
        getDict(dictType) {
            if(this.dictOptions[dictType]){
            	return new Promise((resolve, reject) => {
            		resolve({data:this.dictOptions[dictType]});
            	});
            }
            return getDicts(dictType)
        },
		dictTag(dictType, dictValue) {
			if(!this.dictOptions[dictType]){
				console.log('no dict ' + dictType)
				return '';
			}
			for(let i=0;i<this.dictOptions[dictType].length;i++){
				let dict = this.dictOptions[dictType][i];
				if(dict.dictValue == dictValue){
					return dict.dictLabel;
				}
			}
			console.log(dictType + ' no value ' + dictValue)
		},
		
		// 字典标签转值
		dictValue(dictType, dictlabel) {
			if(!this.dictOptions[dictType]){
				return '';
			}
			for(let i=0;i<this.dictOptions[dictType].length;i++){
				let dict = this.dictOptions[dictType][i];
				if(dict.dictlabel == dictlabel){
					return dict.dictValue;
				}
			}
		}
    }
};

/*
//字典数据
const dictOptions = {};

// 加载字典
function  loadDict(dictType) {
  if(dictOptions[dictType]){
	  return new Promise((resolve, reject) => {
		resolve({data:dictOptions[dictType]});
	  });
  }else{
	  return new Promise((resolve, reject) => {
		  getDicts(dictType).then(response=>{
			  dictOptions[dictType] = response.data;
			  resolve(response);
		  })	  		
	  });
  }  
}

['sys_yes_no', 'asset_status'].forEach(dictType=>{
	loadDict(dictType).then(response => {
		dictOptions[dictType] = response.data;
	  });
})
  
export default {  	
  // 加载字典
  use(dictType) {
	return loadDict(dictType);
	  
	if(Array.isArray(dictTypes)){
		dictTypes.forEach(dictType => {
			loadDict(dictType);
		})
	}else{
		loadDict(dictTypes);
	}
  }, 

  
  // 字典值转标签
  dictTag(dictType, dictValue) {
  	if(!dictOptions[dictType]){
		console.log('no dict ' + dictType)
  		return '';
  	}
  	for(let i=0;i<dictOptions[dictType].length;i++){
  		let dict = dictOptions[dictType][i];
  		if(dict.dictValue == dictValue){
  			return dict.dictLabel;
  		}
  	}
	console.log(dictType + ' no value ' + dictValue)
  },
  
  // 字典标签转值
  dictValue(dictType, dictlabel) {
  	if(!dictOptions[dictType]){
  		return '';
  	}
  	for(let i=0;i<dictOptions[dictType].length;i++){
  		let dict = dictOptions[dictType][i];
  		if(dict.dictlabel == dictlabel){
  			return dict.dictValue;
  		}
  	}
  },
  
  // 字典选项
  dictData(dictType) {
  	return dictOptions[dictType];
  }
}
*/